<?php
/**
 * Created by JetBrains PhpStorm.
 * User: akrug
 * Date: 25.05.12
 * Time: 12:10
 * To change this template use File | Settings | File Templates.
 */
class Backend_OfferController extends UcsMvc_AbstractController
{
    /**
     * @var Backend_Model_DbTable_buscompany
     */
    protected $_mainTable = null;

    /**
     * @var string
     */
    protected $_formSubFolder = '';

    /**
     * @var string
     */
    protected $controllerName = null;

    public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array()){
        parent::__construct($request, $response, $invokeArgs);

        //assoziation zu benutzertabelle
        $this->_mainTable = new Backend_Model_DbTable_Offer();

        //enthaelt den namen controllers - wird z.b. bei fehlermeldungen gebraucht
        $this->_controllerName = 'Offer';

        //ggf. unterordner der vom formular
        $this->_formSubFolder = 'Bus';
    }

    /**
     * listet die unternehmensdaten des eigenen unternehmens auf
     */
    public function myCompanyIndexAction(){
        $myCompany = $this->_mainTable->getCompanyTrimmed($this->_identity->bus_company_nid);

        $this->view->myCompany = $myCompany;
        $this->view->message = $this->_message;
    }

    /**
     * zur verwaltung der unternehmens metadaten des eingeloggten benutzers
     */
    public function myCompanyEditAction(){
        $form = new Backend_Form_Bus_Company();
        $form->addSubForm(new Backend_Form_Confirm(), 'confirm');

        if($this->_hasParam('cancle')){
            $this->_message = $this->_translator->translate('Der Vorgang wurde abgebrochen.', $this->_currentLanguage);
            $this->_forward('my-company-index', null, null, array('message' => $this->_message));
        }elseif($this->_hasParam('submit')){
            if($form->isValid($this->_getAllParams())){

                //datei umbenennen. name enthaelt die id des datensatzes
                $videoConf = Zend_Registry::get('video');
                $form->bus_company_filename->addFilter('rename',
                                       'videos\\'.'test.'.preg_replace('/^video\//', '', $form->bus_company_filename->getMimeType())
                    );
                $form->bus_company_filename->receive();


                //fuegt die companynid hinzu, damit man weiss, welche company geaendert werden muss
                $this->_setParam('id', $this->_identity->bus_company_nid);
                $this->_mainTable->editRow($this->_getAllParams());
                $this->_message = $this->_translator->translate('Der Vorgang wurde erfolgreich durchgeführt.', $this->_currentLanguage);
            }
        }

        //fuellt formular mit vorhandenen daten
        $myCompany = $this->_mainTable->getRow($this->_identity->bus_company_nid);
        $form->populate($myCompany);


        $this->view->message = $this->_message;
        $this->view->form = $form;
    }

    public function uploadAction(){
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        if (isset($_GET['uid'])) {
            $status = uploadprogress_get_info($_GET['uid']);

            if ($status) {
                echo round($status['bytes_uploaded']/$status['bytes_total']*100);
            }
            else {
                echo 100;
            }
        }
    }
}
